package com.demo.service;

import com.demo.properties.EmailProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.internet.MimeMessage;
import java.util.Objects;

/**
 * @author wangfengchen
 */
@Component
public class EmailService {
  private static final Logger log = LoggerFactory.getLogger(EmailService.class);

  @Autowired(required = false)
  private JavaMailSender javaMailSender;
  @Autowired(required = false)
  private EmailProperties emailProperties;

  /**
   * 发送简单邮件
   *
   * @param subject
   * @param content
   */
  public void sendSimpleEmailMessage(String subject, String content) {
    if (javaMailSender != null && emailProperties.getTo() != null
        && emailProperties.getTo().length != 0) {
      //创建一个邮箱消息对象
      SimpleMailMessage message = new SimpleMailMessage();
      //配置邮箱发送人
      message.setFrom(emailProperties.getUsername());
      //邮件的收件人
      message.setTo(emailProperties.getTo());
      if (emailProperties.getCc() != null
          && emailProperties.getCc().length != 0) {
        message.setCc(emailProperties.getCc());
      }
      if (emailProperties.getBcc() != null
          && emailProperties.getBcc().length != 0) {
        message.setBcc(emailProperties.getBcc());
      }
      //邮件的主题
      message.setSubject(subject);
      //邮件的内容
      message.setText(content);
      javaMailSender.send(message);
      log.info("邮件发送成功 = {}", message);
    }
  }

  /**
   * 发送复杂邮件
   *
   * @param subject
   * @param content
   * @param multipartFiles
   */
  public void sendMimeMessage(String subject, String content,
                              MultipartFile[] multipartFiles) {
    if (javaMailSender != null && emailProperties.getTo() != null
        && emailProperties.getTo().length != 0) {
      MimeMessage mailMessage = javaMailSender.createMimeMessage();
      //封装
      MimeMessageHelper helper;
      try {
        //true表示支持复杂类型
        helper = new MimeMessageHelper(mailMessage, true);
        //配置邮箱发送人
        helper.setFrom(emailProperties.getUsername());
        //邮件的收件人
        helper.setTo(emailProperties.getTo());
        if (emailProperties.getCc() != null
            && emailProperties.getCc().length > 0) {
          helper.setCc(emailProperties.getCc());
        }
        if (emailProperties.getBcc() != null
            && emailProperties.getBcc().length > 0) {
          helper.setBcc(emailProperties.getBcc());
        }
        //邮件主题
        helper.setSubject(subject);
        //邮件内容
        helper.setText(content);
        //邮件附件
        if (multipartFiles != null) {
          for (MultipartFile multipartFile : multipartFiles) {
            //addAttachment的两个参数的意思分别是获取邮件中显示的附件的名称，获取附件的资源
            helper.addAttachment(
                Objects.requireNonNull(multipartFile.getOriginalFilename()),
                multipartFile);
          }
        }
        javaMailSender.send(helper.getMimeMessage());
        log.info("邮件发送成功");
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

}

